package com.developtools.doc;

import cn.hutool.core.collection.CollectionUtil;
import com.developtools.doc.markdown.Markdown;
import com.developtools.model.ClassApiInfo;
import com.developtools.model.MethodApiInfo;
import com.developtools.model.ParameterApiInfo;
import com.google.common.collect.Lists;
import com.intellij.diagnostic.logging.LogConsoleManager;
import org.apache.commons.lang3.StringUtils;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Vector;

/**
 * @author liujun
 */
public class MarkdownGenerate implements DocumentGenerate {

    @Override
    public void generate(File destFile, List<ClassApiInfo> contentList) {
        if (Objects.isNull(destFile)) {
            throw new RuntimeException("File is error");
        }

        boolean exists = destFile.exists();
        if (!exists){
            throw new RuntimeException("File is Not found!");
        }

        if (destFile.isDirectory()) {
            directory(destFile, contentList);
        }
    }

    private void directory(File destFile, List<ClassApiInfo> contentList) {
        if (CollectionUtil.isEmpty(contentList)) {
            throw new RuntimeException("Api count is zero!");
        }

        contentList.forEach(clazz -> {
            try {
                String desc = clazz.getDesc();
                if (StringUtils.isBlank(desc)) {
                    throw new RuntimeException("Desc is empty!");
                }
                String filePath = destFile.getAbsolutePath() + "/" + StringUtils.trim(desc) + ".md";
                File file = new File(filePath);
                Markdown markdown = new Markdown();
                markdown.h1(clazz.getDesc());


                List<MethodApiInfo> methodApiInfoList = clazz.getMethodApiInfoList();
                if (CollectionUtil.isEmpty(methodApiInfoList)) {
                    throw new RuntimeException("Not found api record!");
                }
                methodApiInfoList.forEach(methodApiInfo -> {
                    markdown.h2(StringUtils.trim(methodApiInfo.getDesc()));
                    markdown.h3("请求路径");
                    markdown.p(StringUtils.trim(methodApiInfo.getPath()));
                    markdown.h3("请求参数");

                    Markdown.Table table = new Markdown.Table();
                    table.setHeaders(Lists.newArrayList("参数","类型","描述"));

                    List<Vector<String>> cs = new ArrayList<>();
                    List<ParameterApiInfo> parameterApiInfos = methodApiInfo.getParameterApiInfos();
                    parameterLoop(cs, parameterApiInfos);
                    table.setContent(cs);

                    markdown.table(table);

                    markdown.h3("返回参数");
                    Markdown.Table returnTable = new Markdown.Table();
                    returnTable.setHeaders(Lists.newArrayList("参数","类型","描述"));

                    List<Vector<String>> rcs = new ArrayList<>();
                    ParameterApiInfo returnApiInfo = methodApiInfo.getReturnApiInfo();

                    List<ParameterApiInfo> parameters = returnApiInfo.getParameters();
                    if (CollectionUtil.isNotEmpty(parameters)) {
                        parameterLoop(rcs, parameters);
                    }else {
                        Vector<String> returnHead = new Vector<>();
                        returnHead.add(returnApiInfo.getName());
                        returnHead.add(returnApiInfo.getTypeShortName());
                        returnHead.add(StringUtils.trim(returnApiInfo.getDesc()));
                        rcs.add(returnHead);
                    }
                    returnTable.setContent(rcs);
                    markdown.table(returnTable);

                    markdown.h3("返回示例");

                    markdown.code("json", "");
                });

                markdown.write(file);
            }catch (Exception ex){
                throw new RuntimeException("ClassInfo is error, Please add desc ! class[" + clazz.getClassName()+ "]");
            }

        });
    }

    public void parameterLoop(List<Vector<String>> ps, List<ParameterApiInfo> parameterApiInfoList){
        for (ParameterApiInfo parameterApiInfo : parameterApiInfoList) {
            List<ParameterApiInfo> parameters = parameterApiInfo.getParameters();
            if (CollectionUtil.isEmpty(parameters)) {
                Vector<String> vs = new Vector<>();
                vs.add(StringUtils.trim(parameterApiInfo.getName()));
                vs.add(parameterApiInfo.getTypeShortName());
                vs.add(StringUtils.trim(parameterApiInfo.getDesc()));

                ps.add(vs);
            }else {
                parameterLoop(ps, parameters);
            }
        }
    }
}
